package au.org.arcs.dss.provider.datasource.provider.irods;

import java.util.*;

import edu.sdsc.grid.io.GeneralFile;
import edu.sdsc.grid.io.MetaDataRecordList;
import edu.sdsc.grid.io.irods.IRODSFile;

import au.org.arcs.dss.provider.dataset.DataSet;

/**
 *
 * @author ming
 */
public class IRODSDataSetAdapter {
  public static DataSet getDataSet(GeneralFile irodsFile, boolean withParent, boolean withDescendents, boolean withData) {
    DataSet dataSet = getDataSet(irodsFile, withData);

    if (withParent) {
      dataSet.setParent(IRODSDataSetAdapter.getDataSet(irodsFile.getParentFile(), false));
    }

    if (withDescendents) {
      dataSet.setDescendants(IRODSDataSetAdapter.getDataSets(Arrays.asList(irodsFile.listFiles()), false));
    }
    
    return dataSet;
  }

  public static List<DataSet> getDataSets(MetaDataRecordList[] metaDataRecordLists, boolean isDirectory) {
    List<DataSet> dataSets = new ArrayList<DataSet>();

    if (metaDataRecordLists != null && metaDataRecordLists.length > 0) {
      for (MetaDataRecordList metaDataRecordList : metaDataRecordLists) {
        dataSets.add(getDataSet(metaDataRecordList, isDirectory));
      }
    }

    return dataSets;
  }

  public static DataSet getDataSet(MetaDataRecordList metaDataRecordList, boolean isDirectory) {
    long ms = 1000;
    
    DataSet dataSet = new DataSet();

    dataSet.setDirectory(isDirectory);

    if (isDirectory) {
      dataSet.setFile(false);
    } else {
      dataSet.setFile(true);
    }

    dataSet.setName(metaDataRecordList.getStringValue(0));  // name

    if (dataSet.isFile()) {
      dataSet.setPath(IRODSService.getIrodsPath(metaDataRecordList.getStringValue(1), metaDataRecordList.getStringValue(0)));  // path
    } else {
      dataSet.setPath(metaDataRecordList.getStringValue(1)); // path
    }

    if (dataSet.isDirectory()) {
      dataSet.setLastModified(Long.parseLong(metaDataRecordList.getStringValue(2)) * ms);  // lastmodified  irods returns seconds
    } else {
      dataSet.setLength(Long.parseLong(metaDataRecordList.getStringValue(2))); // size
      dataSet.setLastModified(Long.parseLong(metaDataRecordList.getStringValue(3)) * ms);  // lastmodified  irods returns seconds
    }

    return dataSet;
  }

  public static DataSet getDataSet(GeneralFile irodsFile, boolean withData) {
    DataSet dataSet = new DataSet();
    
    dataSet.setName(irodsFile.getName());
    dataSet.setDirectory(irodsFile.isDirectory());

    if (dataSet.isDirectory()) {
      dataSet.setFile(false);
    } else {
      dataSet.setFile(true);
    }

    //dataSet.setFile(irodsFile.isFile());
    dataSet.setLastModified(irodsFile.lastModified());
    dataSet.setPath(irodsFile.getPath());

    if (dataSet.isFile()) {
      dataSet.setLength(irodsFile.length());
    } else {
      dataSet.setLength(0);
    }

    if (withData) {
      if (dataSet.isFile()) {
        if (irodsFile instanceof IRODSFile) {
          dataSet.setFileObject(IRODSService.getBytesFromIRODSFile((IRODSFile) irodsFile));
        }
      }
    }

    return dataSet;
  }

  public static List<DataSet> getDataSets(List<GeneralFile> irodsFiles, boolean withData) {
    List<DataSet> dataSets = new ArrayList<DataSet>();

    if (irodsFiles != null) {
      for (GeneralFile irodsFile : irodsFiles) {
        dataSets.add(getDataSet(irodsFile, withData));
      }
    }
    
    return dataSets;
  }
}